var grpId = getParamValue("grpId");
var params = {grpId:grpId};
var details={};


/*
自定义模板规则：商机信息列表
*/
Handlebars.registerHelper('oppList', function(items, options) {
  var itemArr = new Array();
  if(items instanceof Array){
    itemArr = items;
  }else if(items instanceof Object){
    itemArr.push(items);
  } 
  
  var out = '<table class="am-table am-text-sm">'
        +'<thead>'
        +'<tr>'
        +'<th class="am-text-center">工单编号</th>'
        +'<th class="am-text-center">商机类型</th>'
        +'<th class="am-text-center">商机要素</th>'
        +'<th class="am-text-center">状态</th>'
        +'</tr>'
        +'</thead>'
        +'<tbody class="am-text-center">';
  for(var i=0, l=itemArr.length; i<l; i++) {
    //var item = options.fn(items[i]);
    var item = itemArr[i];
    details[item.id]=item;
    out += "<tr onclick='click2details("+item.id+")'>"
        +'<td>'+item.id+'</td>'
        +'<td>'+item.bus_type+'</td>'
        +'<td>'+item.k2+'</td>'
        +'<td>'+item.k3+'</td>'
        +'</tr>';
  }

  out += '</tbody></table>';

  return out;
});

/**
封装新增按钮和搜索条数据
*/
var getAddAndSearchObj = function(){
  var obj = {};
  obj.addBtnId = "add";
  obj.searchBtnId = "search";
  obj.searchPlaceholder = "查询";

  return obj;
}

var click2details=function(detailsId){
	console.info(detailsId);
	console.info(details[detailsId]);
	setCookie("opp_details",JSON.stringify(details[detailsId]));
	window.location.href= "opportunity_details.html" + "?" + getParams();
	
};

var showDatas = function(datas){
  if(!datas || $.isEmptyObject(datas)){
    showLoadingBar($("#loadingbar"),true,true,"暂无要获取的信息！");
    return;
  }

  showLoadingBar($("#loadingbar"),false);
  
  var $tpl = $('#info-tpl');
  var source = $tpl.text();
  var template = Handlebars.compile(source);
  var myData = {
    list: datas
  };

  var html = template(myData);
  $("#info").html(html);
}

var loadDatas = function(){
  $("#info").html("");
  showLoadingBar($("#loadingbar"),true,false,"加载中，请稍候……");
  var reqUrl = "../../../opponent/getOpportunityList";
  sendDzgPostAjax(params,reqUrl,showDatas,"loadingbar");
};

$(function(){
  //显示新增按钮+搜索条
  showItem(getAddAndSearchObj(),"add-search-tpl");
  //加载商机列表信息
  loadDatas();

  $("#add").on("click",function(){
    window.location.href = "opportunity_add.html" + "?" + getParams();
  });
  
  $("#search").on("click",function(){
    //显示搜索条件弹窗
    $('#my-prompt').modal({
      relatedTarget: this,
      onConfirm: function(e) {
        params.opp_id = $("#opp_id").val();
        params.opp_type = $("#opp_type").val();
        params.opp_part = $("#opp_part").val();

        if(params.opp_type == "全部"){
          params.opp_type = "";
        }
        if(params.opp_part == "全部"){
          params.opp_part = "";
        }

        loadDatas();
      },
      onCancel: function(e) {
        params = {grpId:grpId};
      }
    });
  });
});